package com.woniu.permissions.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.model.Result;
import com.woniu.permissions.form.PageForm;
import com.woniu.permissions.form.UpdateResourceForm;
import com.woniu.permissions.param.PageParam;
import com.woniu.permissions.param.UpdateResourceParam;
import com.woniu.permissions.service.ResourceService;
import com.woniu.util.RedisUtil;
import com.woniu.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.Resources;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@RestController
@RequestMapping("/resource")
@Api(tags = "资源接口")
public class ResourceController {
    @Resource
    private ResourceService resourceService;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 新增资源
     * @param resourceName 资源名称
     * @return
     */
    @ApiOperation("新增资源")
    @GetMapping("/addResource/{resourceName}")
    public Result<com.woniu.permissions.model.Resource> addResource(
            @ApiParam(name = "resourceName",value = "资源名称")
            @PathVariable("resourceName")
            String resourceName,@RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        JwtUtil.parseAdminToken(refreshToken);
        com.woniu.permissions.model.Resource r = resourceService.insertResource(resourceName);
        return Result.newInstance(com.woniu.permissions.model.Resource.class).setData(r).setMessage("新增资源成功");
    }

    /**
     * 根据id删除资源
     * @param id id
     * @return 删除的对象
     */
    @ApiOperation("根据id删除资源")
    @GetMapping("delResource/{id}")
    public Result<com.woniu.permissions.model.Resource> delResource(
            @ApiParam(name = "id",value = "id")
            @PathVariable("id") String id,@RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        JwtUtil.parseAdminToken(refreshToken);
        com.woniu.permissions.model.Resource r = resourceService.delResource(id);
        return Result.newInstance(com.woniu.permissions.model.Resource.class).setData(r).setMessage("删除成功");
    }

    /**
     * 根据id修改资源名
     * @param form
     * @return
     */
    @ApiOperation("根据id修改资源名")
    @PostMapping("/updateResource")
    public Result<com.woniu.permissions.model.Resource> updateResource(@RequestBody UpdateResourceForm form){
        com.woniu.permissions.model.Resource r = resourceService.updateResourceById(BeanUtil.toBean(form, UpdateResourceParam.class));
        return Result.newInstance(com.woniu.permissions.model.Resource.class).setMessage("修改成功");
    }

    /**
     * 分页查询所有资源
     * @param form 页码  每页个数
     * @return 返回查询出的page对象
     */
    @ApiOperation("分页查询所有资源")
    @PostMapping("/selPageResource")
    public Result<Page<com.woniu.permissions.model.Resource>> selPageResource(@RequestBody PageForm form){
        Page<com.woniu.permissions.model.Resource> page = resourceService.selPageResource(BeanUtil.toBean(form, PageParam.class));
        return Result.newPageInstance(com.woniu.permissions.model.Resource.class).setMessage("分页查询成功");
    }

}

